<template>
  <div id="mine">
    <van-nav-bar title="我的"
                 :fixed=true
                 :border=false
                 style="height:2.5rem;" />
    <van-cell-group style="margin-top:2.4rem">
      <van-cell style="background-color: cornflowerblue;color:#FFF;"
                label-class="labelClass"
                is-link
                center>
        <template #title>
          <!-- 已登录状态 -->
          <div class="personMsg"
               v-if="userInfo.token">
            <img class="iconImage"
                 :src="getBaseResUrl+userInfo.headPic"
                 @click="routerTo('/userCenter')"
                 alt="">
            <div class="sex"
                 v-if="userInfo.gender">
              <img :src="userInfo.gender=='0'?userImage.female:userImage.male"
                   alt="">
            </div>
            <div class="personInfo"
                 v-if="userInfo.token">
              <span>{{userInfo.username}}</span>
              <span>{{userInfo.phone}}</span>
            </div>
          </div>
          <!-- 未登录状态 -->
          <div class="personMsg"
               v-if="!userInfo.token">
            <img class="iconImage"
                 :src="userImage.noLoginImage"
                 alt="">
            <div class="personInfo"
                 v-if="!userInfo.token">
              <div @click="goToLogin">立即登录</div>
            </div>
          </div>
        </template>
      </van-cell>
    </van-cell-group>
    <van-cell-group>
      <van-cell :title="'我的订单'"
                icon="label"
                :value="'查看全部订单'"
                is-link to="/orderList">

      </van-cell>
      <van-cell  v-if="userInfo.token"
                :title="'我的钱包'"
                icon="gold-coin"
                :value="'余额:'+accountInfo.balance"
                is-link to="/recharge">

      </van-cell>
      <van-grid direction="horizontal"  :border="false" :column-num="2">
        <van-grid-item icon="video-o" text="电影票" />
        <van-grid-item icon="play-circle-o" text="在线观影" />
      </van-grid>
    </van-cell-group>
    <!--路由的出口-->
    <MyFooter></MyFooter>
  </div>
</template>

<script>
import store from '@/store/index'

import {getJwtToken} from "@/lib/utils/TokenUtil";
import MyFooter from "@/views/layout/MyFooter";

export default {
  name: "user",
  components: {MyFooter},
  data(){
    return{
      userImage: {
        loginImage: require('./../assets/mine/defaultImg.jpeg'),
        noLoginImage: 'https://img01.yzcdn.cn/vant/cat.jpeg',
        female: require('./../assets/mine/female.png'),
        male: require('./../assets/mine/male.png')
      },
      userInfo:{},
      accountInfo:{}
    }
  },
  beforeRouteEnter (to,from,next) {
    if (getJwtToken()!==''){
      if (store.state.user.isGetInfo){
        next(vm =>vm.setUserInfo(store.state.user.info))
      }else {
        console.log('Mine:beforeRouteEnter:GetUserInfo')
        store.dispatch('GetUserInfo').then(res=>{
          next(vm =>vm.setUserInfo(res.data))
        })
      }
    }else{
      next()
    }
  },
  methods:{
    routerTo(path){
      if (this.$route.path!=path){
        this.$router.push(path)
      }
    },
    setUserInfo(data){
        this.userInfo = data
        this.userInfo['token'] = getJwtToken()
        this.accountInfo = store.state.user.accountInfo
    },
    goToLogin(){
      if (this.$route.path!='/login'){
        this.$router.push('/login')
      }
    },
  },
  computed:{
    getBaseResUrl(){
      let staticList = this.$store.state.system.staticData
      let baseUrl = 'http://192.168.0.106:8080/res/'
      for (let item of staticList){
        if (item.paramKey==='BASE_URL_USER_HEAD'){
          baseUrl = item.paramValue
        }
      }
      return baseUrl
    },
  }
}
</script>

<style lang="less">
#mine {
  width: 100%;
  background-color: #f5f5f5;
  margin-bottom: 3rem;
  .version {
    margin: 0 auto;
    text-align: center;
    font-size: 0.6rem;
    background-color: #ffffff;
    height: 2rem;
    color: grey;
    line-height: 2rem;
  }
  .van-nav-bar {
    background-color: cornflowerblue;
    font-size: 0.6rem;
  }
  .van-nav-bar__title {
    color: white;
  }
  .personMsg {
    display: flex;
    align-items: center;
    .sex {
      position: absolute;
      top: 3.5rem;
      left: 3.8rem;
      width: 0.1rem;
      height: 0.1rem;
      img {
        width: 1rem;
        height: 1rem;
      }
    }
    img {
      width: 4rem;
      height: 4rem;
      border-radius: 50%;
    }

    .personInfo {
      display: flex;
      flex-direction: column;
      margin-left: 0.8rem;
    }
  }

  .van-cell__left-icon {
    color:cornflowerblue;
    font-size: 1.2rem;
  }
  /*转场动画*/
  .router-slider-enter-active,
  .router-slider-leave-active {
    transition: all 0.3s;
  }

  .router-slider-enter,
  .router-slider-leave-active {
    transform: translate3d(2rem, 0, 0);
    opacity: 0;
  }
}
</style>

